package com.seed.core.fromaction.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.UUIDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author gchiaway
 * 日期: 2020-08-01
 * 时间: 17:24
 */
@Api(tags = "表单令牌")
@RestController
@RequestMapping("/seed/api/fromAction")
@Slf4j
public class FromActionController {
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 获取请求令牌
     *
     * @return 请求令牌
     */
    @AutoLog(value = "获取请求令牌")
    @ApiOperation(value = "获取请求令牌", notes = "获取请求令牌")
    @GetMapping(value = "/token")
    public Result<String> token() {
        Result<String> result = new Result<>();
        String fromActionToken = UUIDGenerator.generate();
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String fromActionTokenValue = sysUser.getUsername() + System.currentTimeMillis();
        //默认时间是半个小时
        if (!redisUtil.set(fromActionToken, fromActionTokenValue, 1800)) {
            return result.error500("获取请求令牌失败!");
        }
        return result.success("成功", fromActionToken);
    }

}
